<?php

namespace App\Http\Controllers\Mobile;

use App\Http\Requests\RecordStoreFastFormRequest;
use App\Http\Requests\RecordStoreProfileFormRequest;
use App\Http\Requests\RecordStoreAssetFormRequest;
use App\Mail\NotifyAdmin;
use App\Services\Mobile\RecordService;
use App\Services\Mobile\SmsService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;

class RecordController extends Controller
{

    private $recordService;
    private $smsService;

    public function __construct(RecordService $recordService, SmsService $smsService)
    {
        $this->recordService = $recordService;
        $this->smsService = $smsService;
    }

    public function createProfile(Request $request)
    {
        $productId = $request->productId;
        $this->recordService->createProfile($productId);
        return view('mobile.record.createProfile');
    }

    public function storeProfile(RecordStoreProfileFormRequest $request)
    {
        $recordProfile = [
            'fee' => $request->fee,
            'time_limit' => $request->time_limit,
            'city' => $request->city,
            'job' => $request->job,
            'salary_type' => $request->salary_type,
            'salary' => $request->salary,
            'is_social' => $request->is_social,
            'credit' => $request->credit,
            'origin' => $request->origin,
        ];


        $this->recordService->store($request->productId, $recordProfile);

        return redirect(route('record.createAsset'))->with('success', '基本信息提交成功');
    }

    public function createAsset()
    {
        return view('mobile.record.createAsset');
    }

    public function storeAsset(RecordStoreAssetFormRequest $request)
    {
        $recordAsset = [
            'age' => $request->age,
            'sex' => $request->sex,
            'register' => $request->register,
            'is_insurance' => $request->is_insurance,
            'is_house' => $request->is_house,
            'is_car' => $request->is_car,
        ];

        $this->recordService->store(session('productId'), $recordAsset);

        $email = config('front.mail');
        Mail::to($email)->send(new NotifyAdmin((object)['mobile' => session('mobile'), 'create_time' => date('Y-m-d')], $request));


        return redirect(route('record.success'));
    }

    public function createFast()
    {
        return view('mobile.record.createFast');
    }

    public function storeFast(RecordStoreFastFormRequest $request)
    {
        $record = [
            'name' => $request->name,
            'sex' => $request->sex,
            'fee' => $request->fee,
            'time_limit' => $request->time_limit,
            'mobile' => $request->mobile,
        ];
        if (!$this->smsService->verify($record['mobile'], $request->code)) {
            return back()->with('error', '验证码错误');
        }

        $this->recordService->storeFast($record);

        $email = config('front.mail');
        Mail::to($email)->send(new NotifyAdmin((object)['mobile' => session('mobile'), 'create_time' => date('Y-m-d')], $request));
        return redirect(route('record.success'));

    }

    public function success()
    {
        return view('mobile.record.success');
    }
}
